﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.IO;


namespace Controls
{
    /// <summary>
    /// Summary description for SiteCategoryList
    /// </summary>
    public class ProductCategoryList : System.Web.UI.Control
    {
        private int _CurrentCatID = 0;



        public ProductCategoryList()
        {

            if (HttpContext.Current.Request.QueryString["cat"] != null)
            {
                int.TryParse(HttpContext.Current.Request.QueryString["cat"], out _CurrentCatID);
            }
            else
            {
                _CurrentCatID = 0;
            }


        }

        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);


            List<AwinDotNet.ProductCategory> cats = AwinDotNet.AwinAPI.ProductCategories.GetChildCatgories(_CurrentCatID);


            HtmlGenericControl ul = new HtmlGenericControl("ul");
            ul.ID = "NavigationMenu";

            foreach (AwinDotNet.ProductCategory c in cats)
            {
                HtmlGenericControl li = new HtmlGenericControl("li");
                HtmlAnchor anc = new HtmlAnchor();
                anc.HRef = string.Format("listproducts.aspx?cat={0}", c.ID);
                anc.InnerHtml = c.Name;
                anc.Title = c.Name;

                li.Controls.Add(anc);
                ul.Controls.Add(li);
            }

            System.IO.StringWriter sw = new System.IO.StringWriter();
            ul.RenderControl(new HtmlTextWriter(sw));

            writer.Write(sw.ToString());

        }

    }
}